package mn.idax.exchange.otc.service.impl;

import mn.idax.exchange.otc.domain.*;
import mn.idax.exchange.otc.enums.MessageNotifyEnum;
import mn.idax.exchange.otc.service.MongoDBService;
import mn.idax.exchange.otc.service.MongoMessageNotifyService;
import mn.idax.exchange.otc.util.I18nUtil;
import mn.idax.exchange.otc.util.MongoDBQueryUtils;
import mn.idax.exchange.otc.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

/**
 * @Auther: dison
 * @Date: 2018/9/11 13:29
 * @Description:
 */
@Service
public class MongoMessageNotifyServiceImpl implements MongoMessageNotifyService {
    @Autowired
    private MongoDBService mongoDBService;

    @Override
    public MessageNotifyListDTO queryAllMessageByUserId(Integer userId) {
        MessageNotifyListDTO messageNotifyListDTO = new MessageNotifyListDTO();

        ServletRequestAttributes requestAttrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        Query query = this.buildQueryAll(userId , MessageNotifyEnum.system);
        long systemCount = this.mongoDBService.findCount(MessageNotifyDTO.class,query);
        List<MessageNotifyDTO> listSystemMessage = this.mongoDBService.find(query, MessageNotifyDTO.class);

        listSystemMessage.forEach(m -> m.setBody(I18nUtil.getMessage(m.getBody() , ParamUtils.getLanguageByRequest(requestAttrs.getRequest()) , m.getDynamicParam())));

        Query query1 = this.buildQueryAll(userId , MessageNotifyEnum.order);
        long orderCount = this.mongoDBService.findCount(MessageNotifyDTO.class,query1);
        List<MessageNotifyDTO> listOrderMessage = this.mongoDBService.find(query1, MessageNotifyDTO.class);
        listOrderMessage.forEach(m -> m.setBody(I18nUtil.getMessage(m.getBody() , ParamUtils.getLanguageByRequest(requestAttrs.getRequest()) , m.getDynamicParam())));


        messageNotifyListDTO.setOrder(orderCount);
        messageNotifyListDTO.setSystem(systemCount);
        messageNotifyListDTO.setOrders(listOrderMessage);
        messageNotifyListDTO.setSystems(listSystemMessage);

        return messageNotifyListDTO;
    }

    @Override
    public MyMessageNotifyDTO queryPageMessageById(Integer userId, MessageNotifyPageDTO page) {
        MyMessageNotifyDTO myMessageNotify = new MyMessageNotifyDTO();

        ServletRequestAttributes requestAttrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        Query query = this.buildQueryPage(userId);
        List<MessageNotifyDTO> allMessage = this.mongoDBService.find(MessageNotifyDTO.class,query,page.getCurrentPage() + 1,page.getPageSize());
        allMessage.forEach(m -> m.setBody(I18nUtil.getMessage(m.getBody() , ParamUtils.getLanguageByRequest(requestAttrs.getRequest())  , m.getDynamicParam())));

        long count = this.mongoDBService.findCount(MessageNotifyDTO.class,query);

        myMessageNotify.setMessages(allMessage);
        myMessageNotify.setCount(count);
        return myMessageNotify;
    }

    private Query buildQueryAll(Integer userId,MessageNotifyEnum messageNotifyEnum){
        Query query = new Query();
        MongoDBQueryUtils.buildQueryEqualTo(query,
                MessageNotifyEnum.QueryField.receiver , userId,
                MessageNotifyEnum.QueryField.messageType,messageNotifyEnum
        );
        MongoDBQueryUtils.buildQuerySortDesc(query , MessageNotifyEnum.QueryField.sendTime);
        return query;
    }

    private Query buildQueryPage(Integer userId){
        Query query = new Query();
        MongoDBQueryUtils.buildQueryEqualTo(query,
                MessageNotifyEnum.QueryField.receiver , userId
        );
        MongoDBQueryUtils.buildQuerySortDesc(query , MessageNotifyEnum.QueryField.sendTime);
        return query;
    }

    //查询系统消息
    private Query buildQuerySystemPage(Integer userId){
        Query query = new Query();
        query.addCriteria(new Criteria().andOperator(Criteria.where("messageType").is("system")));
        MongoDBQueryUtils.buildQueryEqualTo(query,
                MessageNotifyEnum.QueryField.receiver , userId
        );
        MongoDBQueryUtils.buildQuerySortDesc(query , MessageNotifyEnum.QueryField.sendTime);
        return query;
    }

    //查询订单消息
    private Query buildQueryOrderPage(Integer userId){
        Query query = new Query();
        query.addCriteria(new Criteria().andOperator(Criteria.where("messageType").is("order")));
        MongoDBQueryUtils.buildQueryEqualTo(query,
                MessageNotifyEnum.QueryField.receiver , userId
        );
        MongoDBQueryUtils.buildQuerySortDesc(query , MessageNotifyEnum.QueryField.sendTime);
        return query;
    }

    /**
     * pc用于查询订单消息和系统消息
     */
    @Override
    public MyMessageNotifyDTO queryPagePCMessageById(Integer userId, MessageNotifyPagePCDTO page) {
        MyMessageNotifyDTO myMessageNotify = new MyMessageNotifyDTO();

        ServletRequestAttributes requestAttrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        Query query = new Query();
        if (page.getMessageType() == null){
            query = this.buildQueryPage(userId);
        } else if (page.getMessageType().intValue() == MessageNotifyEnum.MessageType.system.getType()){
            query = this.buildQuerySystemPage(userId);
        } else {
            query = this.buildQueryOrderPage(userId);
        }

        List<MessageNotifyDTO> allMessage = this.mongoDBService.find(MessageNotifyDTO.class,query,page.getCurrentPage() + 1,page.getPageSize());
        allMessage.forEach(m -> m.setBody(I18nUtil.getMessage(m.getBody() , ParamUtils.getLanguageByRequest(requestAttrs.getRequest()) , m.getDynamicParam())));

        long count = this.mongoDBService.findCount(MessageNotifyDTO.class,query);

        myMessageNotify.setMessages(allMessage);
        myMessageNotify.setCount(count);
        return myMessageNotify;
    }
}
